package com.example.computer_exam_training_sys.controller;

import com.example.computer_exam_training_sys.entity.Question;
import com.example.computer_exam_training_sys.service.KnowledgePointService;
import com.example.computer_exam_training_sys.service.QuestionService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/question")
@CrossOrigin
public class QuestionController {

    @Autowired
    private QuestionService service;
    @Autowired
    private KnowledgePointService knowledgePointService;

    // 新增题目
    @PostMapping("/add")
    public String add(@RequestBody Question question) {
        return service.insert(question) > 0 ? "添加成功" : "添加失败";
    }//测试成功

    // 根据ID查询题目
   //  http://localhost:8080/question/get?questionId=500
    @GetMapping("/get")
    public Question getById(@RequestParam Integer questionId) {
        return service.selectById(questionId);
    }//测试成功

    // 根据知识点ID查询题目
    // 访问路径: http://localhost:8080/question/getbypid?pointId=5
    @GetMapping("/getbypid")
    public List<Question> getbypointid(@RequestParam Integer pointId){
        return service.selectBypointId(pointId);

    }

    // 查询全部题目
    @GetMapping("/all")
    public List<Question> getAll() {
        return service.selectAll();
    }//测试成功

    // 更新题目（未测试）
    @PutMapping("/update")
    public String update(@RequestBody Question question) {
        return service.update(question) > 0 ? "修改成功" : "修改失败";
    }

    // 删除题目（未测试）
    @DeleteMapping("/delete/{questionId}")
    public String delete(@PathVariable Integer questionId) {
        return service.delete(questionId) > 0 ? "删除成功" : "删除失败";
    }

    @GetMapping("/by-course")
    public List<Question> getQuestionsByCourse(@RequestParam Integer courseId) {
        // 1. 查该课程下所有知识点
        List<com.example.computer_exam_training_sys.entity.KnowledgePointInfo> points = knowledgePointService.findByCourse(courseId);
        // 2. 查所有知识点下的题目
        List<Question> result = new java.util.ArrayList<>();
        for (com.example.computer_exam_training_sys.entity.KnowledgePointInfo kp : points) {
            List<Question> questions = service.selectBypointId(kp.getPointId());
            result.addAll(questions);
        }
        return result;
    }

    @GetMapping("/allpage")
    public PageInfo<Question> getAllpage(int page, int size) {
        PageInfo<Question> pageInfo = service.findAllpage(page, size);
        return pageInfo;

    }//测试成功

}
//